package com.resou.reader.bookdetail.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.util.GlideRoundTransform;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mindorks.placeholderview.ViewHolder;
import com.resou.reader.R;
import com.resou.reader.activity.friendboost.FriendBoostContract;
import com.resou.reader.api.entry.BaseComment;
import com.resou.reader.api.entry.CommentBean;
import com.resou.reader.bookdetail.comment.release.ReleaseCommentActivity;
import com.resou.reader.bookdetail.listener.OnCommentOnclickListener;
import com.resou.reader.utils.DateUtils;
import com.resou.reader.view.StarRatingBar;

import java.util.List;

/**
 * 书籍详情中书评
 *
 * @author UQIANTU.
 * @date 17/7/7.
 * @最后修改时间 19/4/10
 */

public class BookDetailCommentView extends LinearLayout {
    private Context context;
    private TextView tvComment, tvOpenComment;
    LinearLayout llComment, llemptyPage;
    public OnCommentOnclickListener setCommentOnclickListener;

    public void setCommentOnclickListener(OnCommentOnclickListener commentOnclickListener) {
        this.setCommentOnclickListener = commentOnclickListener;

    }

    public BookDetailCommentView(Context context) {
        super(context);
        init(context);
    }

    public BookDetailCommentView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public BookDetailCommentView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    /**
     * 初始化View
     *
     * @param context
     */
    private void init(Context context) {
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.view_book_detail_comment, this);
        llComment = findViewById(R.id.llComment);
        tvComment = findViewById(R.id.tvComment);
        llemptyPage = findViewById(R.id.llemptyPage);
        tvOpenComment = findViewById(R.id.tvOpenComment);
        tvOpenComment.setText(String.format(context.getResources().getString(R.string.text_open_comment), "0"));

        //绑定事件
        bindEvent();
    }

    /**
     * 绑定事件
     */
    public void bindEvent() {
        tvComment.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (setCommentOnclickListener != null) {
                    setCommentOnclickListener.commentCallback();
                }
            }
        });
        tvOpenComment.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (setCommentOnclickListener != null) {
                    setCommentOnclickListener.lookAllComment();
                }
            }
        });
    }

    /**
     * 放数据
     *
     * @param commentBeans
     * @param bookId
     * @param bookName
     */
    public void setData(List<BaseComment.DataBean> commentBeans, String bookId, String bookName, String
            total) {
        if (commentBeans != null) {
            llemptyPage.setVisibility(View.GONE);
            llComment.setVisibility(View.VISIBLE);
            tvOpenComment.setText(String.format(context.getResources().getString(R.string.text_open_comment), total));


            if (commentBeans.size()>0)
            {
                setView(commentBeans.get(0));
            }
//            int maxNum = commentBeans.size() > 3 ? 3 : commentBeans.size();
//
//            for (int i = 0; i < maxNum; i++) {
//                setView(commentBeans.get(i));
//            }
        } else {
            tvOpenComment.setText(String.format(context.getResources().getString(R.string.text_open_comment), total));
            llemptyPage.setVisibility(View.VISIBLE);
            llComment.setVisibility(View.GONE);
            llemptyPage.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (setCommentOnclickListener != null) {
                        setCommentOnclickListener.commentCallback();
                    }
                }
            });
        }
    }


    /**
     * 设置视图
     *
     * @param commentBean
     * @return
     */
    private void setView(BaseComment.DataBean commentBean) {

        TextView tvUserName = findViewById(R.id.tvUserName);
        LinearLayout llCommentOnclick = findViewById(R.id.llCommentOnclick);
        llCommentOnclick.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setCommentOnclickListener.commentOnclickListener(commentBean.getId() + "");
            }
        });
        tvUserName.setText(commentBean.getNickName() + "");

        TextView tvCommentContent = findViewById(R.id.tvCommentContent);
        tvCommentContent.setText(commentBean.getContent() + "");

        TextView tvTime = findViewById(R.id.tvTime);
        tvTime.setVisibility(View.GONE);
        RequestOptions options = new RequestOptions();
        options.format(DecodeFormat.PREFER_RGB_565);
        Glide.with(context).load(commentBean.getHead_pic()).apply(options).transform(new GlideRoundTransform(context, 360)).into((ImageView)findViewById(R.id.ivUserHead));
    }

}
